#!/usr/bin/python
import vobject
import argparse
import os
import string
import random

parser_arg = argparse.ArgumentParser(description="vCard generator\nExample usage: -n 100 -r -s 10")

parser_arg.add_argument("-n", "--number", type=int,
			required = True,
			help="Number of vCards to be generated")

parser_arg.add_argument("-s", "--start", type=int,
			help="Number of the first user.\n"
			"Name of any user is appended with the int.")

parser_arg.add_argument("-r", "--randomize", action = "store_true",
			help="Randomize the name of the user.")

args = parser_arg.parse_args()

user = 0
numberOfUsers = args.number
override = 1
pathFile = os.path.abspath("contacts_created.vcf")

def randomUserName():
	str = ""
	for x in range(6):
		str += random.choice(string.ascii_letters)
	return str

def randomPhone():
	pNumber = 0
	stringPhone = ""
	for x in range(9):
		pNumber = random.randint(1,9)
		#cast int to string
		stringPhone += str(pNumber)
	return stringPhone

#create a file
if os.path.exists(pathFile):
	print "File is present!"
	print pathFile
	override = input("Do you want to override it? Press 1 if YES!\nOtherwise press any number != 1\n")

#do not override a file if user presses anything except 1
if override != 1:
	print("File not overrided")
	exit()

#Allow user to start the numbering of users from number != 0
if args.start < 1:
	user = 0
	args.start = 0
else:
	user = args.start

if args.randomize == True:
	f = open(pathFile, "w")
	while user < (numberOfUsers + args.start):
		userName  = randomUserName()
		userSurname  = randomUserName()
		phoneNumber = randomPhone()
		f.write("BEGIN:VCARD\n")
		f.write("VERSION:2.1")
		f.write("\n")
		f.write("N:;%s;%s;;;\n" %(userSurname, userName))
		f.write("FN:%s %s\n" %(userName, userSurname))
		f.write("TEL;CELL:%s\n" %phoneNumber)
		f.write("EMAIL;PREF;INTERNET:%s%s@example.com" %(userName, userSurname))
		f.write("\nEND:VCARD\n\n")
		user = user + 1
else:
	f = open(pathFile, "w")
	while user < (numberOfUsers + args.start):
		phoneNumber = randomPhone()
		f.write("BEGIN:VCARD\n")
		f.write("VERSION:2.1")
		f.write("\n")
		f.write("N:;User%d;;;\n" %user)
		f.write("FN:User%d\n" %user)
		f.write("TEL;CELL:%s\n" %phoneNumber)
		f.write("EMAIL;PREF;INTERNET:%s@example.com" %(phoneNumber))
		f.write("\nEND:VCARD\n\n")
		user = user + 1

print("  VCard generated: %s" %pathFile)
